﻿using System;
using System.IO;
using OptixDotNet;
using RayDen.Library.Entity;
using RayDen.Library.Entity.Frames;
using RayDen.Library.Entity.Interface;
using RayDen.Optix.Engine.Components;
using RayDen.Optix.Engine.Engines;
using RayDen.Optix.Engine.Managers;
using RayDen.Optix.Engine.Scene.Factories;

namespace RayDen.Optix.Engine
{
    public class OptixRenderConfig : IRenderConfig, IDisposable
    {
        private ConfigurableEntity config;
        private IRayDenLibraryFrame frame;

        internal RenderEngine Engine;

        public string SavePath
        {
            get { return config.Get("ImageSaveDirectory", "D:\\"); }
        }

        public string FrameName
        {
            get { return frame.FrameName; }
        }

        public int Width
        {
            get;
            set;
        }

        public int Height
        {
            get;
            set;
        }


        public OptixRenderConfig()
        {

            var dirs = ConfigManager.Instance.GetSettings("TextureDirs");

            Engine = new PathTracer();

            EngineContext.Instance.MaterialFactory = new MaterialFactory(EngineContext.Instance.OptixContext);
            EngineContext.Instance.TexManager = new TexManager(EngineContext.Instance.OptixContext, dirs);

        }

        public void Dispose()
        {
            if (EngineContext.Instance.ResourceManager != null)
            {
                EngineContext.Instance.ResourceManager.Dispose();
            }

            if (EngineContext.Instance.TexManager != null)
            {
                EngineContext.Instance.TexManager.Dispose();
            }
            EngineContext.Instance.OptixContext.Dispose();
        }
        public void OpenFrame(FrameDescription frameDescription)
        {
            this.Engine.OpenFrame(frameDescription);
            this.Width = frameDescription.Get<int>("ImageWidth");
            this.Height = frameDescription.Get<int>("ImageHeight");
        }

        public void SaveImage(string fn)
        {
            throw new NotImplementedException();
        }

        public void SaveRawImage(string fn)
        {
            throw new NotImplementedException();
        }


        public void Start()
        {
            this.Engine.StartRendering();
        }

        public void Interrupt()
        {
            this.Engine.StopRendering();
        }

        public void ConsumeJob(IRenderJob job, Action<IRenderJobResult> resultCallback)
        {
            throw new NotImplementedException();
        }

        public string GetStats()
        {
            return string.Empty;
        }


        public void RunCommand(int index)
        {
            throw new NotImplementedException();
        }

        public IImagePlaneComponent GetImagePlane()
        {
            throw new NotImplementedException();
        }
    }
}
